package com.issc.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {
    private static final Logger log= LoggerFactory.getLogger(PropertyUtil.class);

    //加载属性文件
    public static Properties loadProps(String fileName){
        Properties properties;
//        InputStream inputStream=null;
//        inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        properties=new Properties();
        //以文件输入流的形式获取类加载器，并加载
        try( InputStream inputStream=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)) {
            properties.load(inputStream);
        }catch (Exception e){
            log.error("can not load present file",e);
            e.printStackTrace();
//        }finally {
//            if (inputStream!=null){
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
        }
        return properties;
    }
    //获取属性值，通过文件 key获取对应的值
    public static String getString(Properties properties,String key,String defaultValue){
        String value;
        if (properties.containsKey(key)){
            value=properties.getProperty(key);
        }else {
            value=defaultValue;
        }
        return value;
    }

    public static int getInt(Properties properties,String key,int defaultValue){
        int value;
        if (properties.containsKey(key)){
            value = Integer.valueOf(properties.getProperty(key));
        }else {
            value = defaultValue;
        }
        return value;
    }
}
